package basis.game.fx;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;

public class Particle extends Fx{
    
    private double angle,decay=.98,ax,ay,size;
    public double fade=1;
    private Random rand;

    public Particle(double x,double y,double s,int r,int color){
        super(x,y,s,s,-Math.PI,Math.PI,r,255,color);
        rand=new Random((long)(x+y*color)+System.nanoTime());
        angle=Math.toRadians(rand.nextInt(720)/2.);
        double m=rand.nextDouble()*(2+rand.nextInt(5));
        ax=m*Math.cos(angle);
        ay=m*Math.sin(angle);
        size=s;
    }
    
    public Particle(double x,double y,double s,double m,double angle,int r,int color){
        this(x,y,s,r,color,angle);
        ax=m*Math.cos(angle);
        ay=m*Math.sin(angle);
    }
    
    public Particle(double x,double y,double s,int r,int color,double angle){
        this(x,y,s,r,color);
        this.angle=angle;
        double m=rand.nextDouble()*(2+rand.nextInt(5));
        ax=m*Math.cos(angle);
        ay=m*Math.sin(angle);
    }
    
    @Override
    public void update(long t) {
        ax*=decay;
        ay*=decay;
        x+=ax;
        y+=ay;
        fade-=.005;
        size-=.005;
        brightness=(int)(255.*fade);
        if(fade<0){
            fade=0;
            brightness=0;
        }
        if(size<0){
            size=0;
        }
    }

    @Override
    public void render(Graphics2D g) {
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)fade));
        g.setColor(new Color(color));
        g.fill(new Ellipse2D.Double(x,y,size,size));
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
    }
    
}